#ifndef _BSP_USART_H_
#define _BSP_USART_H_

#include <rtconfig.h>
#include <stm32h7xx_hal.h>

/* debug uart begin */

#define DEBUG_UART UART7
#define DEBUG_UART_PIN_GROUP GPIOE
#define DEBUG_UART_TX_PIN GPIO_PIN_8
#define DEBUG_UART_RX_PIN GPIO_PIN_7
#define DEBUG_UART_TX_RX_PIN_AF GPIO_AF7_UART7
#define DEBUG_UART_GPIO_RCC_EN __HAL_RCC_GPIOE_CLK_ENABLE()
#define DEBUG_UART_RCC_EN __HAL_RCC_UART7_CLK_ENABLE()

// #define DEBUG_UART_DMA DMA2
#define DEBUG_UART_TX_DMA_CHANNEL DMA2_Stream5
#define DEBUG_UART_RX_DMA_CHANNEL DMA2_Stream0

#define DEBUG_UART_DMA_TX       /* if defined, means enable uart DMA transfer */
#define DEBUBG_UART_IDLE_DMA_RX /* if defined, means select uart DMA receive with uart rx idle interrupt   \
                                if not defined, means select uart RBNE(receive buffer not empty) interrupt \
                                */

#define DEBUG_UART_TX_BUFFER_SIZE 256
#define DEBUG_UART_RX_BUFFER_SIZE 128

/* Definition for USARTx's NVIC */
#define DEBUG_UART_DMA_TX_IRQn DMA2_Stream5_IRQn
#define DEBUG_UART_DMA_RX_IRQn DMA2_Stream0_IRQn
#define DEBUG_UART_DMA_TX_IRQHandler DMA2_Stream5_IRQHandler
#define DEBUG_UART_DMA_RX_IRQHandler DMA2_Stream0_IRQHandler

#define DEBUG_UART_TX_DMA_STREAM DMA2_Stream5
#define DEBUG_UART_RX_DMA_STREAM DMA2_Stream0

/* Definition for USARTx's DMA Request */
#define DEBUG_UART_TX_DMA_REQUEST DMA_REQUEST_UART7_TX
#define DEBUG_UART_RX_DMA_REQUEST DMA_REQUEST_UART7_RX

#define DEBUG_UART_DMA_CLK_ENABLE __HAL_RCC_DMA2_CLK_ENABLE()
#define DEBUG_UART_IRQn UART7_IRQn
#define DEBUG_UART_HANDLER UART7_IRQHandler

#define DEBUG_UART_FORCE_RESET() __HAL_RCC_UART7_FORCE_RESET()
#define DEBUG_UART_RELEASE_RESET() __HAL_RCC_UART7_RELEASE_RESET()

int bsp_debug_uart_init(void);
int bsp_debug_print_rx_buffer();
int bsp_debug_print_dma(uint8_t *tx_buffer, uint16_t tx_size);
/* debug uart end */

#define DOWNLOAD_UART USART1
#define DOWNLOAD_UART_PIN_GROUP GPIOA
#define DOWNLOAD_UART_TX_PIN GPIO_PIN_9
#define DOWNLOAD_UART_RX_PIN GPIO_PIN_10
#define DOWNLOAD_UART_TX_RX_PIN_AF GPIO_AF7_USART1
#define DOWNLOAD_UART_GPIO_RCC_EN __HAL_RCC_GPIOA_CLK_ENABLE()
#define DOWNLOAD_UART_RCC_EN __HAL_RCC_USART1_CLK_ENABLE()

#define DOWNLOAD_UART_TX_BUFFER_SIZE 512
#define DOWNLOAD_UART_RX_BUFFER_SIZE 128

#define DOWNLOAD_UART_IRQn USART1_IRQn
#define DOWNLOAD_UART_HANDLER USART1_IRQHandler

#define DOWNLOAD_UART_FORCE_RESET() __HAL_RCC_USART1_FORCE_RESET()
#define DOWNLOAD_UART_RELEASE_RESET() __HAL_RCC_USART1_RELEASE_RESET()

#define DOWNLOAD_UART_DMA_TX_IRQn DMA1_Stream1_IRQn
#define DOWNLOAD_UART_DMA_RX_IRQn DMA1_Stream0_IRQn
#define DOWNLOAD_UART_DMA_TX_IRQHandler DMA1_Stream1_IRQHandler
#define DOWNLOAD_UART_DMA_RX_IRQHandler DMA1_Stream0_IRQHandler

#define DOWNLOAD_UART_TX_DMA_STREAM DMA1_Stream1
#define DOWNLOAD_UART_RX_DMA_STREAM DMA1_Stream0

/* Definition for USARTx's DMA Request */
#define DOWNLOAD_UART_TX_DMA_REQUEST DMA_REQUEST_USART1_TX
#define DOWNLOAD_UART_RX_DMA_REQUEST DMA_REQUEST_USART1_RX

#define DOWNLOAD_UART_DMA_CLK_ENABLE __HAL_RCC_DMA1_CLK_ENABLE()
#define DOWNLOAD_UART_IRQn USART1_IRQn
#define DOWNLOAD_UART_HANDLER USART1_IRQHandler

int bsp_download_uart_init(void);
int bsp_download_print_rx_buffer();
int bsp_download_send_tx_buffer(uint8_t* tx_buf, uint16_t tx_cnt);
/*  */

#ifdef RT_USING_DEVICE
int init_debug_uart_device(void);
#endif

#endif
